#include "widget_begin.h"
#include "widget_setup_local.h"
#include "widget_setup_network.h"
#include <string>
#include <iostream>



added_player::added_player( player* pl, QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	QHBoxLayout* layout = new QHBoxLayout( this );
	layout->setSpacing( 50 );

	QLabel* player_l = new QLabel( this );
	player_l->setTextFormat( Qt::RichText );
	std::ostringstream sout;
	sout << "<b><font size=5>Player " << pl->get_id() << ":</font></b>";
	player_l->setText( sout.str().c_str() );
	layout->addWidget( player_l );
	sout.str( "" );

	QLabel* name_l=new QLabel( this );
	name_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>" << pl->get_name() << "</font>";
	name_l->setText( sout.str().c_str() );
	layout->addWidget( name_l );
	sout.str( "" );

	QLabel* type_l = new QLabel( this );
	type_l->setTextFormat( Qt::RichText );
	switch ( pl->get_type() )
	{
	case ai:
		type_l->setText( "<font size=5>AI player</font>" );
		break;
	case local:
		type_l->setText( "<font size=5>Human player</font>" );
		break;
	case network:
		type_l->setText( "<font size=5>Network player</font>" );
		break;
	}
	layout->addWidget( type_l );

	QLabel* color_l = new QLabel( this );
	color_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>" << game_gui::get_instance()->graphics->get_color( pl->get_id() ) << " color</font>";
	color_l->setText( sout.str().c_str() );
	layout->addWidget( color_l );

	this->setLayout( layout );
};






begin_widget::begin_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{	
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );

	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Basic Setup</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	next_b->setEnabled( false );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	QLabel* game_type_l = new QLabel( this );
	game_type_l->setTextFormat( Qt::RichText );
	game_type_l->setText( "<b><font size=6>Game type</font></b>" );
	layout->addWidget( game_type_l );


	QWidget* game_type_w = new QWidget( this );
	QHBoxLayout* game_type_w_l = new QHBoxLayout( game_type_w );

	local_rb=new QRadioButton( "Local", game_type_w );
	local_rb->setChecked( true );
	game_type_w_l->addWidget( local_rb );



	network_rb=new QRadioButton( "Network", game_type_w );
	game_type_w_l->addWidget( network_rb );

	game_type_w->setLayout( game_type_w_l );
	layout->addWidget( game_type_w );


	layout->addSpacing( 20 );


	QLabel* add_l = new QLabel( this );
	add_l->setTextFormat( Qt::RichText );
	add_l->setText( "<b><font size=6>Adding of new players</font></b>" );
	layout->addWidget( add_l );


	QWidget* add_w = new QWidget( this );
	QHBoxLayout* add_w_l = new QHBoxLayout( add_w );
	add_w_l->setSpacing( 20 );

	QLabel* name_l = new QLabel( add_w );
	name_l->setTextFormat( Qt::RichText );
	name_l->setText( "<font size=5>Name:</font></b>" );
	add_w_l->addWidget( name_l );

	name_i = new QLineEdit( add_w );
	name_i->setFixedWidth( 250 );
	add_w_l->addWidget( name_i );	

	QLabel* type_l = new QLabel( add_w );
	type_l->setTextFormat( Qt::RichText );
	type_l->setText( "<font size=5>Type:</font></b>" );
	add_w_l->addWidget( type_l );

	type_i = new QComboBox( add_w );
	type_i->addItem( "Human" );
	type_i->addItem( "AI" );
	add_w_l->addWidget( type_i );
	
	add_b = new QPushButton( "Add player", add_w );
	add_w_l->addWidget( add_b );
	connect( add_b, SIGNAL( clicked() ), this, SLOT( add_player() ) );

	add_w->setLayout( add_w_l );
	layout->addWidget( add_w );


	layout->addSpacing( 20 );


	this->setLayout( layout );
};



void begin_widget::add_player()
{
	players* pla = players::get_instance();
	player* pl;
	std::string nm = name_i->text().toStdString();	
	if ( nm == "" )
	{
		std::ostringstream sout;
		sout << "Player " << pla->get_players_count();
		nm = sout.str();
	}
	PlayerType typ;
	if ( type_i->currentText() == "AI" )
		typ = ai;
	else if ( type_i->currentText() == "Human" )
		typ = local;

	pl = new player( pla->get_players_count(), nm, typ );
	pla->set_player( pl, pla->get_players_count() );
	added_player* ap = new added_player( pl, this );
	layout->addWidget( ap );
	name_i->setText( "" );
	if ( pla->get_players_count() >= parameters::get_instance()->game_parameters[ "MAX_PL" ] )
		add_b->setEnabled( false );
	if ( pla->get_players_count() >= 2 )
		next_b->setEnabled( true );
};



void begin_widget::next()
{
	game_gui* ggui = game_gui::get_instance();
	MyQScrollArea* sa = new MyQScrollArea( ggui->g );
	if ( local_rb->isChecked() )
		ggui->g->setup_w = new local_setup_widget( sa );
	else
		ggui->g->setup_w = new network_setup_widget( sa );
	sa->setSizeHint( ggui->g->setup_w->size() );
	sa->setWidget( ggui->g->setup_w );
	sa->setWidgetResizable( true );
	ggui->g->setCentralWidget( sa );
};